package cn.circle.system.util;


import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

/**
 * 功能：统一提供时间工具 2016/11/12 作者：吴泽磊
 * 
 */
public class DateUtils {

	public static String getDateResult(Long time) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日 hh时mm分ss秒");
		Date dt = new Date(time * 1000);
		String result_time = sdf.format(dt);
		return result_time;
	}
	//获取当前的时间与1970年1月1日00:00的相差毫秒值
	public static Long getNowFrom1970(){
    	Date dt= new Date();
  	  	Long time= dt.getTime();//这就是距离1970年1月1日0点0分0秒的毫秒数
  	  	//System.out.println(System.currentTimeMillis());//与上面的相同
  	  	//System.out.println(time);
  	  	return time;
	}
	//获取当前时间的string，精确到秒，不过还是会出现同一秒同时上传同一张图片的情况，在文件名后面加了个5位数的随机字符串
	public static String getNowDateString(){
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		Calendar calendar = Calendar.getInstance();
		String imageName = df.format(calendar.getTime());
		return imageName;
	}
	// string转long
	public static long getTimestamp(String sTime) {
		Timestamp ts = Timestamp.valueOf(sTime);
		Date date2 = null;
		try {
			date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("1970/01/01 08:00:00");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long l = ts.getTime() - date2.getTime() > 0 ? ts.getTime() - date2.getTime() : date2.getTime() - ts.getTime();
		long rand = (int) (l / 1000);

		return rand;
	}

	public static String getCreateTime() {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String CREATE_TIME = df.format(new Date());
		return CREATE_TIME;

	}

	public static String getYearMonth() {

		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		String CREATE_TIME = df.format(new Date());
		return CREATE_TIME;

	}

	// String 转date
	public static Date getStringToDate(String str) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;

	}

	// 日期格式字符串+1
	public static String getDateAddDay(String str) {

		Date date = null;
		try {
			date = (new SimpleDateFormat("yyyy-MM-dd")).parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, 1);
		str = (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
		return str;

	}
	


}

